#ifndef __MY_CONFIG__
#define __MY_CONFIG__
#include <mutex>
#include "Util.hpp"
#define CONFIG_FILE "./Cloud.conf"
namespace Cloud
{
    class Config // 服务器配置文件单例类
    {
    private:
        Config()
        {
            ReadConfig(CONFIG_FILE);
        }
        static Config *_instance;
        static mutex _mutex;
        // 备份信息配置管理
    private:
        time_t hot_time;        // 定义热点时间
        uint16_t server_port;   // 服务器端口
        string server_ip;       // 服务器ip
        string download_prefix; // 下载文件路径前缀
        string packfile_suffix; // 压缩包后缀
        string pack_dir;        // 压缩包存放路径
        string back_dir;        // 备份文件存放路径
        string backup_file;     // 服务端备份信息存放⽂件
    public:
        bool ReadConfig(const string &filename) // 加载配置文件信息
        {
            Cloud::FileUtil fu(filename);
            string body;
            // 获取文件内容
            if (fu.GetCentent(&body) == false)
            {
                cout << "load Config file failed" << endl;
                exit(-1);
            }
            Json::Value root;
            Cloud::JsonUtil::UnSerialize(body, &root); // 对读取到的内容反序列化
            hot_time = root["hot_time"].asInt();
            server_port = root["server_port"].asInt();
            server_ip = root["server_ip"].asString();
            download_prefix = root["download_prefix"].asString();
            packfile_suffix = root["packfile_suffix"].asString();
            pack_dir = root["pack_dir"].asString();
            back_dir = root["back_dir"].asString();
            backup_file = root["backup_file"].asString();
            return true;
        }
        static Config *GetInstance()
        {
            if (_instance == nullptr)
            {
                _mutex.lock();
                if (_instance == nullptr)
                {
                    _instance = new Config;
                }
                _mutex.unlock();
            }
            return _instance;
        }
        int GetHotTiem()
        {
            return hot_time;
        }
        int GetServerPort()
        {
            return server_port;
        }
        string GetServerIp()
        {
            return server_ip;
        }
        string GetPackFileSuffix()
        {
            return packfile_suffix;
        }
        string GetDownLoadprefix()
        {
            return download_prefix;
        }
        string GetBackDir()
        {
            return back_dir;
        }
        string GetPackDir()
        {
            return pack_dir;
        }
        string GetBackUpFile()
        {
            return backup_file;
        }
    };
    Config *Config::_instance = nullptr;
    mutex Config::_mutex;
}

#endif